<html>
<head><meta charset="utf-8"><title>Freeze · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html">Freeze</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="173204689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173204689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173204689">(Aug 14 2019 at 13:53)</a>:</h4>
<blockquote>
<blockquote>
<p>(I think we should consider exposing Freeze)</p>
</blockquote>
<p>@centril what problem would that solve that can't be solved with UnsafeCell&lt;T&gt; ?</p>
</blockquote>
<p>See <a href="https://github.com/rust-lang/rust/issues/60715" target="_blank" title="https://github.com/rust-lang/rust/issues/60715">https://github.com/rust-lang/rust/issues/60715</a></p>



<a name="173205213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205213">(Aug 14 2019 at 13:59)</a>:</h4>
<p>that makes sense</p>



<a name="173205234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205234">(Aug 14 2019 at 13:59)</a>:</h4>
<p>I thought somebody wanted to expose it in such a way that it can be implemented for their own types</p>



<a name="173205491"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205491" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205491">(Aug 14 2019 at 14:02)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I guess that could make sense if you have some type that is <code>!Freeze</code> that you've wrapped inside another type which you want to be <code>Freeze</code> but you've ensured that no interior mutation is possible through the wrapper type</p>



<a name="173205495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205495">(Aug 14 2019 at 14:02)</a>:</h4>
<p>idk how useful that is</p>



<a name="173205550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205550">(Aug 14 2019 at 14:02)</a>:</h4>
<p><code>Freeze</code> can also be used to create an owned type which does not allow mutation at all</p>



<a name="173205567"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205567" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205567">(Aug 14 2019 at 14:03)</a>:</h4>
<p>no matter if moved to a new binding or not</p>



<a name="173205880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205880">(Aug 14 2019 at 14:06)</a>:</h4>
<p>I'm having a hard time imagine which problems these would solve</p>



<a name="173205979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173205979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173205979">(Aug 14 2019 at 14:07)</a>:</h4>
<p>well the latter is useful as a sort of "let's really restrict mutation for code maintainability purposes";<br>
the former swym usecase is more compelling tho</p>



<a name="173206076"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173206076" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173206076">(Aug 14 2019 at 14:08)</a>:</h4>
<p>the swym usecase of using <code>T: Freeze</code> as bound feels ok</p>



<a name="173206163"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173206163" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173206163">(Aug 14 2019 at 14:09)</a>:</h4>
<p>the case for implementing <code>Freeze</code> manually is probably not compelling, but just wait for someone to do something weird :P</p>



<a name="173206176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173206176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173206176">(Aug 14 2019 at 14:10)</a>:</h4>
<p>but using <code>T: !Freeze</code> as bound doesn't feel that useful,<br>
implementing <code>Freeze</code> for types that would be <code>!Freeze</code> feels like maaybe,</p>



<a name="173206232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173206232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173206232">(Aug 14 2019 at 14:10)</a>:</h4>
<p>implementing <code>!Freeze</code> for types that would be <code>Freeze</code> feels wrong</p>



<a name="173206409"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173206409" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173206409">(Aug 14 2019 at 14:12)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Foo</span><span class="p">(</span><span class="kt">u32</span><span class="p">);</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="o">!</span><span class="n">Freeze</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{}</span><span class="w">  </span><span class="c1">// Foo === UnsafeCell&lt;Foo&gt; ?</span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">next</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">u32</span> <span class="p">{</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">};</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="173207494"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173207494" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173207494">(Aug 14 2019 at 14:24)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> the usecase for implementing <code>!Freeze</code> manually is mostly to avoid committing (in the semver sense) to being <code>Freeze</code>; you can use <code>PhantomData&lt;Cell&lt;()&gt;&gt;</code> for that tho.</p>
<p>As for negative bounds that isn't a thing...?</p>



<a name="173207869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173207869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173207869">(Aug 14 2019 at 14:28)</a>:</h4>
<p>I thought you can have a generic impl for T, and then specialize it for T: Freeze</p>



<a name="173207887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173207887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173207887">(Aug 14 2019 at 14:29)</a>:</h4>
<p>and now you have separated !Freeze from Freeze</p>



<a name="173209116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173209116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173209116">(Aug 14 2019 at 14:43)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> maybe something like <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=94a031ddac7b2c5185292531f3f58f46" target="_blank" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=94a031ddac7b2c5185292531f3f58f46">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=94a031ddac7b2c5185292531f3f58f46</a> but rustc refuses to make that work</p>



<a name="173209185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173209185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173209185">(Aug 14 2019 at 14:44)</a>:</h4>
<p>but why would you use <code>T: !Freeze</code> as a bound... I could see using <code>T: Freeze</code> as a bound for optimization purposes or for correctness</p>



<a name="173210175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210175">(Aug 14 2019 at 14:55)</a>:</h4>
<p>same question, i don't think doing that would be useful</p>



<a name="173210475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210475">(Aug 14 2019 at 14:59)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> it's clearly useful in the swym case...?</p>



<a name="173210519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210519">(Aug 14 2019 at 15:00)</a>:</h4>
<p>They use <code>T: Freezw</code>, not <code>T: !Freeze</code>, right? Otherwise I completely misunderstood what they were doing</p>



<a name="173210594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210594">(Aug 14 2019 at 15:00)</a>:</h4>
<p>I thought they wanted to use <code>T: Freeze</code> to avoid using types containing <code>UnsafeCell</code></p>



<a name="173210602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210602">(Aug 14 2019 at 15:00)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> I think we are talking past each other; we both agree that bounding on <code>!Freeze</code> is not useful</p>



<a name="173210609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210609">(Aug 14 2019 at 15:00)</a>:</h4>
<p>yep</p>



<a name="173210624"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210624" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210624">(Aug 14 2019 at 15:00)</a>:</h4>
<p>bounding on <code>T: Freeze</code> seems definetely useful</p>



<a name="173210628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210628">(Aug 14 2019 at 15:00)</a>:</h4>
<p>yep</p>



<a name="173210638"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210638" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210638">(Aug 14 2019 at 15:01)</a>:</h4>
<p>implementing Freeze or !Freeze might be useful</p>



<a name="173210652"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210652" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210652">(Aug 14 2019 at 15:01)</a>:</h4>
<p>i would be fine with allowing people to play with that behind a different feature gate</p>



<a name="173210658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210658">(Aug 14 2019 at 15:01)</a>:</h4>
<p>I wonder if there are any interesting optimizations you can do through specialization with <code>T: Freeze</code></p>



<a name="173210676"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze/near/173210676" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Freeze.html#173210676">(Aug 14 2019 at 15:01)</a>:</h4>
<p>Yep; we should also audit the compiler's use of the lang item</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>